/**
 * Created by Administrator on 2017/3/13.
 */
//$("#footer").load("common/footer.html");
$(function(){
    updateCart();
    //ajax获取商品详情
    $.get(
        commonURL + 'op=goods_detail',
        {
            id: getQueryString('id')
        },
        function (res) {
            var data = JSON.parse(res);
            console.log(data);
            sessionStorage.setItem(getQueryString('id'),res);
            loadDetail(data);
        }
    );
    //适用门店
    $.get(
        commonURL + 'op=store',
        {
            id: getQueryString('id')
        },
        function (res) {
            var data = JSON.parse(res);
            if(data){
                var html='';
                for(var i=0;i<data.length;i++){
                    html+='<li><div><i class="iconal-gongsi"></i>'+data[i].storename+'</div><div>地址:'+data[i].address+'</div><div>电话:'+data[i].tel+'</div></li>';
                }
                $('.stores_list>ul').html(html);
            }else{
                $('.stores').css('display','none');
            }
        }
    );
    //加载购物车数据
    //$.post(ajaxCar,function(res){
    //    var data=JSON.parse(res);
    //    if(data.length==0){return false;}
    //    var num= 0,times= 0,prices=0;
    //    for(var i=0;i<data.length;i++){
    //        num+=data[i].num-0;
    //        times+=Number(data[i].num)*Number(data[i].item_endtime);
    //        prices+=Number(data[i].num)*Number(data[i].oprice);
    //    }
    //    $('.iconal-gouwuche>b').html(num);
    //    $('.cartSum_price>b').html(prices.toFixed(2));
    //    $('.cartSum_num>p:first-child>span').html(num);
    //    $('.cartSum_num>p:last-child>span').html(times);
    //    $('.cartSum').css('display','flex').css('display','-webkit-flex');
    //})
});


///**适用门店**/
$('.stores>a.weui-cell').click(function(){
    var span=$('.stores>a.weui-cell>span');
    if(span.hasClass('iconal-xiangxiajiantou')){
        span.removeClass('iconal-xiangxiajiantou').addClass('iconal-xiangshangjiantou');
        $('.stores_list').css('display','block');
    }else{
        span.removeClass('iconal-xiangshangjiantou').addClass('iconal-xiangxiajiantou');
        $('.stores_list').css('display','none');
    }
});
//选购商品
$('.footerDetail_jj>.minus').click(function(){
    var num=$('.footerDetail_jj>.num');
   if(num.html()==0){
        return false;
   }else{
       if(num.html()=='1'){
           $('.footerDetail_js span').html('去选购商品').next().html('').parent().attr('data-href','index.html');
       }
       num.html(num.html()-1);
   }
});
$('.footerDetail_jj>.add').click(function(){
    var num=$('.footerDetail_jj>.num');
    if(num.html()==0) {
        $('.footerDetail_js span').html('去结算').next().html('￥0.01').parent().attr('data-href','index_order.html');
    }
        num.html(num.html()-0+1);
});
$('.footerDetail_js .backgd').click(function(){
   location.href=$(this).attr('data-href');
});




//加载商品详情页的所有信息
function loadDetail(data){
    //获取轮播图片
    if(data.advs.length==0){
        $('#sliderImg img').attr('src',data.gimg)
    }else{
        var imgHtml='',numHtml='';
        for(var j=0;j<data.advs.length;j++){
            imgHtml+="<li><img src="+data.advs[j].thumb+" style='width:100%;'></li>";
            if(j==0){
                numHtml+='<span class="curr"></span>';
            }else{
                numHtml+='<span></span>';
            }
        }
        $('#sliderImg ul').html(imgHtml);
        $('#sliderNum').html(numHtml);
        new Swipe($('#sliderImg')[0], {
            speed: 500,
            auto: 3000,
            callback: function () {
                var lis = $("#sliderNum").children();
                lis.removeClass("curr").eq(this.index).addClass("curr");
            }
        });
    }
    //获取属性
    $('.detail li b').html(data.oprice);
    $('.detail li s').html('￥'+data.mprice);
    $('.detail .iconal-zuyue').next().html(data.endtime);
    $('.detail .iconal-qunzuduoren').next().html(data.salenum);
    $('.detail>div').html(data.gname+'<br/><p style="color: #aaa;font-size: 14px">'+data.goodsdesc+'</p>');
    if(data.gdesc==''){
        $('.detail_imgs').html('暂无项目详情').css('text-align','center').css('padding','5px 0');
    }else{
        $('.detail_imgs').html(escape2Html(data.gdesc));
    }
    $('.yuebiao').attr('data-id',data.id).attr('data-type',data.deliverytype).attr('data-params',JSON.stringify(data.params));
}

function escape2Html(str){
    var arrEntities={'lt':'<','gt':'>','nbsp':' ','amp':'&','quot':'"'};
    return str.replace(/&(lt|gt|nbsp|amp|quot);/ig,function(all,t){return arrEntities[t];});
}

function yuebiaoClick1(a,e){
    var self=a;
    $.get(
        addCar,
        {id:getQueryString('id')},
        function(data) {
            if(data==1) {
                location.href=urlorder;
                var detail=JSON.parse(sessionStorage.getItem(getQueryString('id')));
                console.log(detail);
                //$('.cartSum').css('display','flex').css('display','-webkit-flex');
                var timeN=detail.endtime;
                var priceN=detail.oprice;
                $("i.iconal-gouwuche>b").html($("i.iconal-gouwuche>b").html()-0+1);//数量
                $(".cartSum_price>b").html(($(".cartSum_price>b").html()-0+Number(priceN)).toFixed(2));//价格
                $('.cartSum_num>p:first-child>span').html($("i.iconal-gouwuche>b").html());//项目
                $('.cartSum_num>p:last-child>span').html($('.cartSum_num>p:last-child>span').html()-0+Number(timeN));//时间
            }else{
                $.toast('购买失败','text')
            }
        })
}









